package com.mystudy.IO;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ws.schild.jave.*;

import java.io.File;
import java.io.FilenameFilter;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/14 19:15
 * @Description 计算文件夹内map4文件的总时间长度
 */
public class CalculateMp4Length {
    private static final Logger LOGGER = LoggerFactory.getLogger(CalculateMp4Length.class);
    private static final int MER = 102424;

    @Test
    public void calculate() throws EncoderException {
        String fileName = "D:\\BaiduNetdiskDownload\\玩转数据结构 从入门到进阶\\第11章 并查集";
        calculate(fileName);
    }


    private void calculate(String fileName) throws EncoderException {
        File dir = new File(fileName);
        if (!dir.exists()) {
            LOGGER.error("该文件夹不存在");
            return;
        }
        File[] mp4s = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.indexOf("mp4") != -1;
            }
        });
        long sum = 0;
        for (int i = 0; i < mp4s.length; i++) {
            File source = mp4s[i];
            try {
                MultimediaObject instance = new MultimediaObject(source);
                MultimediaInfo result = instance.getInfo();
                long ls = result.getDuration() / 1000;
//                int hour = (int) (ls / 3600);
//                int minute = (int) (ls % 3600) / 60;
//                int second = (int) (ls - hour * 3600 - minute * 60);
//                length = hour + "'" + minute + "''" + second + "'''";
                sum = sum + ls;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int hour = (int) (sum / 3600);
        int minute = (int) (sum % 3600) / 60;
        int second = (int) (sum - hour * 3600 - minute * 60);
        String length = "";
        length = hour + "'" + minute + "''" + second + "'''";
        LOGGER.warn("length = [{}]",length);
    }

}
